import { Item } from '@/pages/InnerEditTable/index.d';
import moment from 'moment';
import _ from 'lodash';
import { dataFormat1 } from '@/untils/dictionaries';
const initTableData: Item[] = [];
for (let i = 0; i < 100; i++) {
  initTableData.push({
    key: i.toString(),
    projectID: '项目编号' + i,
    projectName: '项目名字' + i,
    projectAddress: `London Park no. ${i}`,
    projectPrincipal: ['负责人' + i],
    projectMoney: i * 100 + 1,
    projectStartTime: moment('2020-06-10', dataFormat1),
    projectStartTimeLabel: '2022-06-07',
    serviceContent: ['服务内容' + i],
    serviceType: '服务类型' + i,
    projectStatus: i,
    projectStatusLabel: '状态' + i,
    operator: '操作员' + i,
    operaTime: '操作时间' + i,
  });
}
export default {
  namespace: 'InnerEditTable',
  state: {
    isModalVisible: false,
    tableData: initTableData,
    selectedRowKeys: [],
    total: initTableData.length,
  },
  reducers: {
    changeTableData(state: any, { payload }: any) {
      return {
        ...state,
        tableData: payload,
        total: payload.length,
      };
    },
    changeSelectedRowKeys(state: any, { payload }: { payload: string[] }) {
      //选中列表
      return {
        ...state,
        selectedRowKeys: payload,
      };
    },
    tableDataDeletes(state: any) {
      state.selectedRowKeys.forEach((key: string) => {
        const idx = state.tableData.findIndex((item: Item) => {
          return item.key === key;
        });
        idx > -1 && state.tableData.splice(idx, 1);
        console.log(idx);
      });
      const newTableData = _.cloneDeep(state.tableData);
      return {
        ...state,
        selectedRowKeys: [],
        tableData: newTableData,
        total: newTableData.length,
      };
    },
    addTableData(state: any, { payload }: { payload: Item }) {
      state.tableData.push(payload);
      const newTableData = _.cloneDeep(state.tableData);
      return {
        ...state,
        tableData: newTableData,
        total: newTableData.length,
      };
    },
    setIsModalVisible(state: any, { payload }: { payload?: string[] }) {
      //选中列表
      return {
        ...state,
        isModalVisible: payload || !state.isModalVisible,
      };
    },
  },
};
